<?php

declare(strict_types=1);

namespace app\api\model;

use app\api\model\Section as ModelSection;
use app\api\model\Week as ModelWeek;
use app\api\model\Teacher as ModelTeacher;
use app\api\model\Stage as ModelStage;
use think\facade\Request;
use app\api\service\UserToken;

/**
 * 章节模型
 */
class Chapter extends Base
{
    protected $autoWriteTimestamp = 'datetime';

    /**
     * # 获取周期下章节
     * @param string|array|int $week_ids
     * @param string $order
     * @param string $option
     * @return false|object
     */
    public static function getChapter($week_ids, $option = 'select',$order = 'desc')
    {
        
        $field = 'id,title,is_lock,create_time';
        $where = [];
        if (is_string($week_ids) || is_int($week_ids)) {
            $where[] = ['week_id', '=', $week_ids];
        } else if (is_array($week_ids)) {
            $where[] = ['week_id', 'in', $week_ids];
        } else {
            return false;
        }
        $chapter = Chapter::where($where)->field($field)->order('id',$order)->$option();
        return $chapter;
    }

    /**
     * # 获取老师创建的章节
     * @param string|int $teacher_id
     * @param int|false $is_lock
     * @return object
     */
    public static function getTeachCreateChapter($teacher_id,$is_lock = false,$order = 'desc')
    {
        $field = 'id,title,is_lock,create_time';
        $where[] = ['user_id','=',$teacher_id];
        if($is_lock !== false){
            $where[] = ['is_lock','=',$is_lock];
        }
        $chapter = Chapter::where($where)->field($field)->order('id',$order)->select();
        return $chapter;
    }

    /**
     * # 修改章节状态
     * @param object|array $chapter
     * @param string|int $status
     * @param bool $bool
     * @return true
     */
    public static function editChapter($chapter, $status, $bool = false)
    {
        if ($bool === true) {
            $is_lock = $status == 0 ? 1 : 0;
            $chapter->is_lock = $is_lock;
        } else {
            $chapter->title = $status;
        }
        $success = $chapter->save();
        return $success;
    }


    /**
     * # 删除章节
     * @param object $chapter
     * @return true
     */

    public static function delChapter($chapter)
    {
        $success = $chapter->delete();
        return $success;
    }

    /**
     * # 新增章节
     * @param array $data
     * @return object
     */

    public static function addChapter($data)
    {
        $success = Chapter::create($data);
        return $success;
    }

    /**
     * 
     * 获取章节小节
     */
    public static function getSection($chapter_id)
    {
        $chapter = Chapter::find($chapter_id);
        if (empty($chapter)) {
            return [
                'code' => 400,
                'message' => '参数错误',
            ];
        }
        $data = ModelSection::where('chapter_id', $chapter_id)->field('id,title,status')->select()->toArray();
        return [
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $data
        ];
    }


    /***
     * 
     * 获取未发布章节总数
     */
    public static function getNoPublicChapterNumber($course_id)
    {
        $chapter = Chapter::where('is_lock', 1)->select()->toArray;
        if (empty($chapter)) {
        } else {
            return count($chapter);
        }
    }

    /**
     * # 验证章节是否存在
     * @param string|int $chapter_id
     * @return array
     */
    public static function verifyChapter($chapter_id)
    {
        $chapter = Chapter::find($chapter_id);
        if (empty($chapter)) {
            return false;
        }
        return $chapter;
    }


    public static function onAfterDelete($user)
    {
        if ($user->is_lock != 0) {
            return;
        }
        //修改教师发布课程数量
        $teacher = ModelTeacher::verifyTeacher($user->user_id);
        $teacher->dec('chapter_count')->update();
    }


    public static function onAfterUpdate($user)
    {
        if ('editTitle' == Request::action()) {
            return;
        }
        //修改教师发布课程数量
        $teacher = ModelTeacher::verifyTeacher($user->user_id);
        if ($user->is_lock == 0) {
            $teacher->inc('chapter_count')->update();
        } else if ($user->is_lock == 1) {
            $teacher->dec('chapter_count')->update();
        };
    }

    public static function onAfterInsert($user)
    {
        //修改教师发布课程数量
        $teacher = ModelTeacher::verifyTeacher($user->user_id);
        if ($teacher === false) {
            return;
        }
        $chapter = Chapter::getTeachCreateChapter($user->id,0);
        if (empty($chapter->toArray())) {
            $teacherInfo['chapter_count'] = 0;
        } else {
            $teacherInfo['chapter_count'] = count($chapter);
        }
        $teacher->save();
    }
}
